package com.lcj.controller;


import com.lcj.dto.ArticleCondition;
import com.lcj.dto.ArticleInfo;

import com.lcj.dto.CommentDto;
import com.lcj.dto.CommentsDto;
import com.lcj.rsp.ApiResponse;
import com.lcj.service.IArticleService;
import com.lcj.utils.JwtService;
import com.lcj.utils.UserUtils;
import com.lcj.vo.ArticleInfoVo;
import com.lcj.vo.ArticlesNBVo;
import com.lcj.vo.ArticlesVo;
import com.lcj.vo.CommentInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;


@RestController
@RequestMapping("/api")
public class ArticleController {

    @Autowired
    private IArticleService articleService;
    @Autowired
    JwtService jwtService;

    @PostMapping("/articles")
    public String createArticles(@RequestBody ArticleInfo articleInfo, HttpServletRequest request) {
        Long loginUserId = UserUtils.getLoginUserId();
        articleService.createArticle(articleInfo, loginUserId);
        return "success";
    }

    @GetMapping("/articles/{aid}")
    public ApiResponse<ArticleInfoVo> getArticleBySlug(@PathVariable Long aid) {
        ArticleInfoVo article = articleService.getArticle(aid);
        return ApiResponse.success(article);

    }

    @GetMapping("/articles")
    public ApiResponse<ArticlesVo> getAllArticles(ArticleCondition articleCondition) {
        if (articleCondition.getLimit() == null) {
            articleCondition.setLimit(10);
        }
        if (articleCondition.getOffset() == null) {
            articleCondition.setOffset(0);
        }
        ArticlesVo allArticles = articleService.getAllArticles(articleCondition);
        return ApiResponse.success(allArticles);
    }
    @GetMapping("/articles/feed")
    public ApiResponse<ArticlesNBVo> getArticlesFeed() {
        ArticlesNBVo articles = articleService.getARticlesByFeed();
        return ApiResponse.success(articles);
    }
    @PutMapping("/articles/{aid}")
    public ApiResponse<ArticleInfoVo> updateArticle(@PathVariable Long aid, @RequestBody ArticleInfo articleInfo) {
        ArticleInfoVo article = articleService.updateArticle(aid,articleInfo);

        return ApiResponse.success(article);
    }
    @DeleteMapping("/articles/{aid}")
    public ApiResponse deleteArticle(@PathVariable Long aid) {
        articleService.deleteArticle(aid);
        return ApiResponse.success();
    }
//    POST /api/articles/:slug/comments
    @PostMapping("/articles/{aid}/comments")
    public ApiResponse<CommentDto> createComments(@PathVariable Long aid, @RequestBody CommentInfoVo comment) {
        CommentDto comments = articleService.createComments(aid, comment);
        return ApiResponse.success(comments);
    }
//    GET /api/articles/:slug/comments
    @GetMapping("/articles/{aid}/comments")
    public ApiResponse<CommentsDto> getComments(@PathVariable Long aid) {
        CommentsDto comments = articleService.getComments(aid);
        return ApiResponse.success(comments);
    }
//    DELETE /api/articles/:slug/comments/:id
    @DeleteMapping("/articles/{aid}/comments/{cid}")
    public ApiResponse deleteComments(@PathVariable Long aid, @PathVariable Long cid) {
        articleService.deleteComments(aid, cid);
        return ApiResponse.success();
    }
    //POST /api/articles/:slug/favorite
    @PostMapping("/articles/{aid}/favorite")
    public ApiResponse<ArticleInfoVo> favoriteArticle(@PathVariable Long aid) {
        ArticleInfoVo articleInfoVo = articleService.favoriteArticle(aid);
        return ApiResponse.success(articleInfoVo);

    }
    //DELETE /api/articles/:slug/favorite
    @DeleteMapping("/articles/{aid}/favorite")
    public ApiResponse<ArticleInfoVo> unFavoriteArticle(@PathVariable Long aid) {
        ArticleInfoVo articleInfoVo = articleService.unFavoriteArticle(aid);
        return ApiResponse.success(articleInfoVo);
    }
}
